package com.wshl.cloudservice.ui.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ScreenUtils;

public class DragFrameLayout extends FrameLayout {
//    private int mLastX = ConvertUtils.dp2px(10);
//    private int mLastY = ConvertUtils.dp2px(60);

    //是否拖动
    private boolean isDrag = false;

    public boolean isDrag() {
        return isDrag;
    }

    private int mLastX;
    private int mLastY;

    public DragFrameLayout(@NonNull Context context) {
        super(context);
    }

    public DragFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

    }

    public int getStatusBarHeight() {
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        return getResources().getDimensionPixelSize(resourceId);
    }

    /**
     * 让view能够随手自由拖动
     *
     * @param event
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        int x = (int) event.getRawX(); //触摸点相对于屏幕的横坐标
        int y = (int) event.getRawY(); //触摸点相对于屏幕的纵坐标
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                isDrag = false;
                break;
            case MotionEvent.ACTION_MOVE: //当手势类型为移动时

                int deltaX = x - mLastX; //两次移动的x距离差
                int deltaY = y - mLastY;//两次移动的y的距离差
                if (Math.abs(deltaX) > 10||Math.abs(deltaY) > 10) {
                    isDrag = true;
                }


                //重新设置此view相对父容器的偏移量
                int screenHeight = ScreenUtils.getScreenHeight();
                int screenWidth = ScreenUtils.getScreenWidth();

                int translationX = (int) getTranslationX() + deltaX;
                int translationY = (int) getTranslationY() + deltaY;

                LogUtils.i("X：" + translationX + ";y:" + translationY);
                //限制不能划出边界
                if (translationX <-ConvertUtils.dp2px(10)) {
                    translationX = -ConvertUtils.dp2px(10);
                } else if (translationX > (screenWidth - ConvertUtils.dp2px(100))) {
                    translationX = (screenWidth - ConvertUtils.dp2px(100));
                }

                if (translationY < -ConvertUtils.dp2px(86)) {
                    translationY = -ConvertUtils.dp2px(86);
                } else if (translationY > (screenHeight - ConvertUtils.dp2px(238))) {
                    translationY = (screenHeight - ConvertUtils.dp2px(238));
                }

                setTranslationX(translationX);
                setTranslationY(translationY);
                break;
            case MotionEvent.ACTION_UP:
                setPressed(false);
                break;
            case MotionEvent.ACTION_CANCEL:
                setPressed(false);
                break;
            default:
                break;
        }
        //记录上一次移动的坐标
        mLastX = x;
        mLastY = y;
        return true;
    }


}
